package com.example.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.app.api.Apiservice;
import com.example.app.interfaces.ResponseCallBack;
import com.example.app.viewmodel.Goods;
import com.example.app.viewmodel.detail.Detail;
import com.example.app.viewmodel.detail.Info;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

public class GoodsInfo extends AppCompatActivity {
    private static final String EXTRA_ID = "id";  // 用于标识传递的参数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_info);

        com.youth.banner.Banner detail_info_gallery = findViewById(R.id.detail_info_gallery);
        TextView detail_info_retailPrice = findViewById(R.id.detail_info_retailPrice);
        TextView detail_info_counterPrice = findViewById(R.id.detail_info_counterPrice);
        TextView detail_info_name = findViewById(R.id.detail_info_name);
        TextView detail_info_brief = findViewById(R.id.detail_info_brief);
        TextView detail_info_detail = findViewById(R.id.detail_info_detail);

        Button goods_back = findViewById(R.id.goods_back);
        goods_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent(GoodsInfo.this, MainActivity.class);
                startActivity(returnIntent);
                finish(); // 结束当前活动
            }
        });

        //获取数据
        Intent intent = getIntent();
        int itemId = intent.getIntExtra(EXTRA_ID,1001020);
        Apiservice.GetGoods(new ResponseCallBack<Goods<Detail>>() {
            @Override
            public void Success(Goods<Detail> responseData) {
                Info info = responseData.getData().getInfo();

                //1.轮播图
                detail_info_gallery.setAdapter(new BannerImageAdapter<String>(info.getGallery()) {
                            @Override
                            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                                //图片加载自己实现
                                Glide.with(holder.imageView)
                                        .load(data)
                                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                        .into(holder.imageView);
                            }
                        })
                        .addBannerLifecycleObserver(GoodsInfo.this)//添加生命周期观察者
                        .setIndicator(new CircleIndicator(GoodsInfo.this));

                //2.价格 描述
                detail_info_retailPrice.setText("¥ " + info.getRetailPrice() + "");
                detail_info_counterPrice.setText(info.getCounterPrice() + "");
                detail_info_name.setText(info.getName());
                detail_info_brief.setText(info.getBrief());

                //3.图片
                detail_info_detail.setText(info.getDetail());
            }
        },itemId);
    }

}